
package teamCservlets;
import java.io.Serializable;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.sql.Types;



public class DatabaseInterfaceApplication extends DatabaseInterface {

	// constructor.

	public DatabaseInterfaceApplication () {
		super();
	} // End constructor

	// The getApplication method loads an Application by application ID.
	// If an application with the specified ID is found, it returns an
	// ApplicationBean object.  Otherwise it returns "null".
	public synchronized ApplicationBean getApplication(int appID) {

		try {

			String appQuery = new String("SELECT * FROM APPLICATION WHERE Application_ID = ?");

			LogInfo(appQuery);

			preparedStatement = connection.prepareStatement(appQuery);

			preparedStatement.setInt(1,appID);

			runSQLstatement();


			// If result is null, return null.  Otherwise, generate a ApplicationBean
			// object and return it.

			if(resultSet == null) {
				LogWarning("DatabaseInterfaceApplication: Result set is null");
				return null;
			} else {

				// Check for empty resultSet.

				if(!resultSet.isBeforeFirst()) {
					LogWarning("DatabaseInterfaceApplication: The resultSet is empty for the application query.");
					return null;
				}
				// Create new ApplicationBean object


				ApplicationBean app = new ApplicationBean();
				resultSet.first();
				// Get the query results.
				app.setApplicationID(resultSet.getInt("Application_ID"));

				app.setApplicationName(resultSet.getString("Application_Name"));

				// Close the statement and connection.

				cleanup();


				return app;
			} // End code that returns ApplicationBean

		} catch (SQLException sqle) {

			LogException(sqle);
			return null;
		} catch (NullPointerException npe) {

			LogException(npe);
			return null;
		} catch (Exception e) {
			LogException(e);
			return null;
		}

	} // End getApplication method

	// The getApplicationList method retrieves all entries from the Application
	// table and provides an ArrayList of ApplicationBean.
	public synchronized ArrayList<ApplicationBean> getApplicationList() {

		try {

			ArrayList<ApplicationBean> appList = new ArrayList<ApplicationBean>();

			String appQuery = new String("SELECT Application_ID, Application_Name FROM APPLICATION");

			preparedStatement = connection.prepareStatement(appQuery);

			runSQLstatement();

			// If result is null, return null.  Otherwise, create a list of all applications.
			if(resultSet == null) {
				LogWarning("DatabaseInterfaceApplication: Array Result set is null");
				return null;
			} else {
				// For each row returned...
				while (resultSet.next()) {

					// Create a new ApplicationBean and store the appId and appName in it
					ApplicationBean app = new ApplicationBean();
					app.setApplicationID(resultSet.getInt("Application_ID"));
					app.setApplicationName(resultSet.getString("Application_Name"));

					// Add the ApplicationBean to the ArrayList
					appList.add(app);

				}

				// Close the statement and connection.

				cleanup();

				return appList;
			} // End code that builds the application list

		} catch (SQLException sqle) {

			LogException(sqle);
			return null;
		} catch (NullPointerException npe) {

			LogException(npe);
			return null;
		} catch (Exception e) {
			LogException(e);
			return null;
		}

	} // End getApplicationList method

} // End DatabaseInterface